package cn.iocoder.yudao.module.forward.mqtt;

import cn.iocoder.yudao.module.forward.mqtt.broker.BrokerMessageHandler;
import cn.iocoder.yudao.module.forward.mqtt.broker.BrokerMqttManager;
import cn.iocoder.yudao.module.forward.mqtt.dataSource.SourceMessageHandler;
import cn.iocoder.yudao.module.forward.mqtt.dataSource.SourceMqttManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

@Slf4j
@Configuration
public class MqttConfig {
    @Bean(name = MqttConstants.THREAD_POOL_MQTT_RECONNECT)
    public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        //设置线程池大小
        taskScheduler.setPoolSize(10);
        //线程名称前缀
        taskScheduler.setThreadNamePrefix("mqtt_reconnect-pool-");
        //设置终止时等待最大时间，单位s，即在关闭时，需等待其他任务完成执行
        taskScheduler.setAwaitTerminationSeconds(3000);
        //设置关机时是否等待计划任务完成，不中断正在运行的任务并执行队列中的所有任务，默认false
        taskScheduler.setWaitForTasksToCompleteOnShutdown(true);
        return taskScheduler;
    }

    @Bean(name = "SourceMqttManager")
    public MqttManager initSourceMqttManager() {
        return new SourceMqttManager();
    }

    @Bean(name = "BrokerMqttManager")
    public MqttManager initBrokerMqttManager() {
        return new BrokerMqttManager();
    }

    @Bean(name = "SourceMessageHandler")
    public SourceMessageHandler initSourceMessageHandler() {
        return new SourceMessageHandler();
    }

    @Bean(name = "BrokerMessageHandler")
    public BrokerMessageHandler initBrokerMessageHandler() {
        return new BrokerMessageHandler();
    }
}
